﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExamSystem.BLL;
using ExamSystem.Mdeol;
namespace ExamSystem.qt
{
    public partial class TQuestion_List : System.Web.UI.Page
    {
        private TpagerBLL tb = new TpagerBLL();
        private TagerstudentBLL tbl = new TagerstudentBLL();
        private TanswerBLL tab = new TanswerBLL();
        protected List<Tanswer> list;
        protected Tanswer tanswer;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                string tsno = Session["uid"].ToString();
                List<Tagerstudent> list1 = tbl.quareytager(tsno);
                if (list1 != null)
                {
                    foreach (Tagerstudent t in list1)
                    {
                        this.lbTpName.InnerText = t.TpId.TpName;
                        this.lbTpBeginTime.InnerText = t.TpId.TpBeginTime;
                        this.lbTpEndTime.InnerText = t.TpId.TpEndTime;
                        this.lbTpScore.InnerText = t.TpId.Tpscore + "";
                        this.lbTpCount.InnerText = t.TpId.Tpcount + "";
                    }
                }

                //查询试题
                this.queryquestion();

                //点击试题按钮定位试题位置
                string paramquestionid = Request.QueryString["paramQuestionId"];
                if(!string.IsNullOrEmpty(paramquestionid))
                {
                    for (int i = 0; i < list.Count;i++ )
                    {
                        Tanswer ta = list[i];
                        if(ta.TqId.TqId==int.Parse(paramquestionid))
                        {
                            this.currentDateIndex.Value = i + "";
                            int index = int.Parse(this.currentDateIndex.Value);
                            tanswer = list[index];

                            //试题id
                            this.currentId.Value = tanswer.TqId.TqId + "";
                            //试题答案
                            this.answer.Value = tanswer.TaAnswer;
                            break;
                        }
                    }
                }




                //提交试卷
                string op = Request.QueryString["op"];
                if(!string.IsNullOrEmpty(op)&&op=="commit")
                {
                    string tpid = Request.QueryString["id"];
                    string tsid =Session["tsid"].ToString();
                    if(!string.IsNullOrEmpty(tpid)&&!string.IsNullOrEmpty(tsid))
                    {
                        tbl.updatepagerstate(int.Parse(tpid), int.Parse(tsid));
                        Response.Redirect("TPager_List.aspx");
                        return;
                    }
                    else
                    {
                        util.FunHelper.LayerAlter(this,"id非法");
                    }
                   
                }
                //点击试题按钮定位试题位置
                string paramquestionid1 = Request.QueryString["paramQuestionId"];
            }
            
           
        }
        //查询试题
        public void queryquestion()
        {
            int paid = int.Parse(Request.QueryString["id"]);
            int tsid = int.Parse(Session["tsid"].ToString());
            list = tab.querybypageridandstuid(paid,tsid);
            int index = int.Parse(this.currentDateIndex.Value);
            tanswer = list[index];


            this.currentId.Value = tanswer.TqId.TqId + "";
            this.dataCount.Value = list.Count + "";
            this.answer.Value = tanswer.TaAnswer;
        }

        //更新答案
        public void updateanswer()
        {
            int questionid = int.Parse(this.currentId.Value);
            int tpid = int.Parse(Request.QueryString["id"]);
            int tsid = int.Parse(Session["tsid"].ToString());
            string answer = this.answer.Value;
            tab.updatastuanswer(tpid,questionid,tsid,answer);
        }

        //下一题
        protected void btnNext_Click(object sender, EventArgs e)
        {
            this.updateanswer();
            int index = int.Parse(this.currentDateIndex.Value);
            index++;
            int count = int.Parse(this.dataCount.Value);
            if(index>=count)
            {
                index = 0;
            }
            this.currentDateIndex.Value = index + "";
            this.queryquestion();
        }
        //上一题
        protected void btnLast_Click(object sender, EventArgs e)
        {
            this.updateanswer();
            int index = int.Parse(this.currentDateIndex.Value);
            index--;
            int count = int.Parse(this.dataCount.Value);
            if (index < 0)
            {
                index = count - 1 ;
            }
            this.currentDateIndex.Value = index + "";
            this.queryquestion();
        }

    }
}